package com.crack.nga2.custom;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import com.crack.nga2.R;

/**
 * 刷新布局
 * Created by Administrator on 2015/12/18.
 */
public class HeadLayoutImpl implements ILoadingLayout, Animation.AnimationListener {
    private Context context;
    private View rootView;
    private View barLeft, barRight;
    /**
     * 设置屏幕宽度像素值
     */
    public static int WIDTH = 480;
    private boolean isVisible;
    private boolean isRefresh;
    private View back;
    private View[] left = new View[2];
    private View[] right = new View[2];
    private int[] colors = new int[3];
    private Animation aLeft, aRight;

    public HeadLayoutImpl(Context context) {
        this.context = context;
        rootView = View.inflate(context, R.layout.pull_header, null);
        setViews();
        setAnimation();
    }

    public HeadLayoutImpl(Context context, View rootView) {
        this.rootView = rootView;
        this.context = context;
        setViews();
        setAnimation();
    }

    private void setViews() {
        barLeft = rootView.findViewById(R.id.bar_left);
        barRight = rootView.findViewById(R.id.bar_right);
        barRight.scrollTo(WIDTH / 2, 0);
        barLeft.setVisibility(View.INVISIBLE);
        barRight.setVisibility(View.INVISIBLE);
        rootView.setVisibility(View.INVISIBLE);
        isVisible = false;
    }

    private void setAnimation() {
        int width = WIDTH;
        colors[0] = context.getResources().getColor(R.color.flush_1);
        colors[1] = context.getResources().getColor(R.color.flush_2);
        colors[2] = context.getResources().getColor(R.color.flush_3);
        back = rootView.findViewById(R.id.back);
        left[0] = rootView.findViewById(R.id.left0);
        left[1] = rootView.findViewById(R.id.left1);
        right[0] = rootView.findViewById(R.id.right0);
        right[1] = rootView.findViewById(R.id.right1);
        aLeft = new TranslateAnimation(0, -width / 2, 0, 0);
        aLeft.setDuration(500);
        aLeft.setAnimationListener(this);
        aRight = new TranslateAnimation(0, width / 2, 0, 0);
        aRight.setDuration(500);
    }

    @Override
    public void init(View rootView) {
    }

    @Override
    public View getView() {
        return rootView;
    }

    @Override
    public void pullTo(int x) {
        if (!isVisible) {
            rootView.setVisibility(View.VISIBLE);
            isVisible = true;
        }
        int offset = x / 2;
        barLeft.setVisibility(View.VISIBLE);
        barRight.setVisibility(View.VISIBLE);
        barLeft.scrollTo(offset, 0);
        barRight.scrollTo(WIDTH / 2 - offset, 0);
        rootView.setPadding(0, 0, 0, x / 4);
    }

    @Override
    public void stopPull() {
        barLeft.scrollTo(0, 0);
        barRight.scrollTo(WIDTH / 2, 0);
        rootView.setPadding(0, 0, 0, 0);
    }

    @Override
    public void startRefresh() {

        rootView.setVisibility(View.VISIBLE);
        isVisible = true;
        isRefresh = true;
        left[0].setBackgroundColor(colors[0]);
        left[0].setVisibility(View.VISIBLE);
        right[0].setBackgroundColor(colors[0]);
        right[0].setVisibility(View.VISIBLE);
        back.setBackgroundColor(colors[1]);
        back.setVisibility(View.VISIBLE);
        left[0].startAnimation(aLeft);
        right[0].startAnimation(aRight);
    }

    @Override
    public void stopRefresh() {
        isRefresh = false;
    }


    private int moveIndex = 0;
    private int backIndex = 1;

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

        //需要隐藏的View下标
        int hideIndex = moveIndex;
        //需要显示的（马上开始动画的）View的下标
        moveIndex = (moveIndex + 1) % 2;
        //动画颜色(跟上一次动画的背景颜色相同)
        int moveColor = backIndex;
        //背景颜色下标变换
        backIndex = (backIndex + 1) % 3;
        //隐藏
        left[hideIndex].setVisibility(View.INVISIBLE);
        right[hideIndex].setVisibility(View.INVISIBLE);
        right[hideIndex].clearAnimation();
        //设置颜色
        left[moveIndex].setBackgroundColor(colors[moveColor]);
        right[moveIndex].setBackgroundColor(colors[moveColor]);
        //显示
        right[moveIndex].setVisibility(View.VISIBLE);
        left[moveIndex].setVisibility(View.VISIBLE);
        //设置背景颜色
        back.setBackgroundColor(colors[backIndex]);
        if (!isRefresh) {
            //执行最后一次动画
            lastAnimation();
            return;
        }
        //开始动画
        left[moveIndex].startAnimation(aLeft);
        right[moveIndex].startAnimation(aRight);
    }


    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    private void lastAnimation() {
        //停止刷新 最后执行一次动画 并且隐藏所有的动画View
        Animation aLeft = new TranslateAnimation(0, -WIDTH / 2, 0, 0);
        aLeft.setDuration(500);
        aLeft.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                hideViews(left);
                hideViews(right);
                hideViews(back);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        back.setVisibility(View.INVISIBLE);
        left[moveIndex].startAnimation(aLeft);
        right[moveIndex].startAnimation(aRight);
    }

    private void hideViews(View... views) {
        for (View view : views) {
            view.setVisibility(View.INVISIBLE);
        }
    }
}
